import Blockly from 'blockly/core'
import 'blockly/python_compressed'
import 'blockly/blockly_compressed'
import 'blockly/blocks_compressed'

Blockly.Blocks.with_keyword = {
  init: function () {
    this.appendValueInput('NAME').setCheck(null).appendField('with')
    this.appendStatementInput('SENTENCE').setCheck(null)
    this.setPreviousStatement(true, null)
    this.setNextStatement(true, null)
    this.setColour(230)
    this.setTooltip('')
    this.setHelpUrl('')
  }
}

Blockly.Python.with_keyword = function (block) {
  const valueName = Blockly.Python.valueToCode(
    block,
    'NAME',
    Blockly.Python.ORDER_ATOMIC
  )
  const statementSentence = Blockly.Python.statementToCode(block, 'SENTENCE')
  let code
  if (!statementSentence) {
    code = 'with ' + valueName
  } else {
    code = 'with ' + valueName + '\n' + statementSentence
  }
  return code
}

export default Blockly
